﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Light.Utility.HtmlTable
{
    public class Table
    {
        
        private Alignment _align = Alignment.Default;
        private Color _alternateRowBackColor = Color.Empty;
        private Color _backColor = Color.Empty;
        private int _border = 0;
        private int _cellPadding = 0;
        private int _cellSpacing = 0;
        private int _columnCount = 0;
        private Row _columnHeader = null;
        private ColumnCollection _columns = null;
        private FontSetter _fontSetter = null;
        private bool _overrideRowAdornments = false;
        private RowCollection _rows = null;
        private Alignment _tableAlignment = Alignment.Default;
        private string _title = string.Empty;

        
        public Table()
        {
            this._cellSpacing = 0;
            this._cellPadding = 0;
            this._tableAlignment = Alignment.Center;
            this._border = 1;
            this._backColor = Color.White;
            this._align = Alignment.Left;
            this._alternateRowBackColor = Color.FromArgb(0xef, 0xef, 0xef);
            this._fontSetter = new FontSetter();
            this._fontSetter.FontColor = Color.Black;
            this._fontSetter.FontFace = "Tahoma";
            this._fontSetter.FontSize = 8;
            this._fontSetter.Bold = false;
            this._fontSetter.Italic = false;
            this._columnHeader = new Row();
            this._columnHeader.FontSetter.FontColor = Color.Black;
            this._columnHeader.FontSetter.FontFace = "Tahoma";
            this._columnHeader.FontSetter.FontSize = 8;
            this._columnHeader.FontSetter.Bold = true;
            this._columnHeader.FontSetter.Italic = false;
            this._columnHeader.BackColor = Color.LightYellow;
            this._columnHeader.Align = Alignment.Center;
            this._columnHeader.OverrideCellAdornments = false;
            this._rows = new RowCollection();
            this._rows.Table = this;
            this._columns = new ColumnCollection();
            this._overrideRowAdornments = false;
        }

        internal void SetRowIndex()
        {
            if (this._rows != null)
            {
                for (int i = 0; i < this._rows.Count; i++)
                {
                    this._rows[i].Table = this;
                    this._rows[i].RowIndex = i;
                }
            }
        }

        
        public Alignment Align
        {
            get
            {
                return this._align;
            }
            set
            {
                if (this.OverrideRowAdornments)
                {
                    for (int i = 0; i < this.Rows.Count; i++)
                    {
                        this.Rows[i].Align = Alignment.Default;
                    }
                }
                this._align = value;
            }
        }

        public Color AlternateRowBackColor
        {
            get
            {
                return this._alternateRowBackColor;
            }
            set
            {
                this._alternateRowBackColor = value;
            }
        }

        public Color BackColor
        {
            get
            {
                return this._backColor;
            }
            set
            {
                if (this.OverrideRowAdornments)
                {
                    for (int i = 0; i < this.Rows.Count; i++)
                    {
                        this.Rows[i].BackColor = Color.Empty;
                    }
                }
                this._backColor = value;
            }
        }

        public int Border
        {
            get
            {
                return this._border;
            }
            set
            {
                this._border = value;
            }
        }

        public int CellPadding
        {
            get
            {
                return this._cellPadding;
            }
            set
            {
                this._cellPadding = value;
            }
        }

        public int CellSpacing
        {
            get
            {
                return this._cellSpacing;
            }
            set
            {
                this._cellSpacing = value;
            }
        }

        public int ColumnCount
        {
            get
            {
                return this._columnCount;
            }
            set
            {
                this._columnCount = value;
                for (int i = 0; i < this._columnCount; i++)
                {
                    this.Columns.Add(new Column(i, this));
                }
            }
        }

        public Row ColumnHeader
        {
            get
            {
                return this._columnHeader;
            }
            set
            {
                this._columnHeader = value;
            }
        }

        public ColumnCollection Columns
        {
            get
            {
                return this._columns;
            }
            set
            {
                this._columns = value;
            }
        }

        public FontSetter FontSetter
        {
            get
            {
                return this._fontSetter;
            }
            set
            {
                if (this.OverrideRowAdornments)
                {
                    for (int i = 0; i < this.Rows.Count; i++)
                    {
                        this.Rows[i].FontSetter = FontSetter.Default;
                    }
                }
                this._fontSetter = value;
            }
        }

        public bool OverrideRowAdornments
        {
            get
            {
                return this._overrideRowAdornments;
            }
            set
            {
                this._overrideRowAdornments = value;
            }
        }

        public RowCollection Rows
        {
            get
            {
                return this._rows;
            }
            set
            {
                this._rows = value;
                this.SetRowIndex();
            }
        }

        public Alignment TableAlignment
        {
            get
            {
                return this._tableAlignment;
            }
            set
            {
                this._tableAlignment = value;
            }
        }

        public string Title
        {
            get
            {
                return this._title;
            }
            set
            {
                this._title = value;
            }
        }
    }




}
